package com.example.videolib

import android.content.res.Configuration
import android.graphics.SurfaceTexture
import android.util.Log
import android.view.TextureView
import android.view.View
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import androidx.test.internal.runner.junit4.statement.UiThreadStatement.runOnUiThread
import com.example.baselib.activity.BaseActivity
import com.example.baselib.fragment.BaseFragment
import com.hikvision.open.hikvideoplayer.HikVideoPlayer
import com.hikvision.open.hikvideoplayer.HikVideoPlayerCallback
import com.hikvision.open.hikvideoplayer.HikVideoPlayerFactory
import kotlinx.android.synthetic.main.fragment_play_video.*
import java.text.MessageFormat

class VideoPlayFragment : BaseFragment<VideoViewModel>() , HikVideoPlayerCallback {
    override fun providerVMClass(): Class<VideoViewModel>? =VideoViewModel::class.java

    override fun contentLayout(): Int {
        return  R.layout.fragment_play_video
    }

    override fun onPlayerStatus(status: HikVideoPlayerCallback.Status, i: Int) {
        if (status == HikVideoPlayerCallback.Status.FAILED || status == HikVideoPlayerCallback.Status.EXCEPTION) {
            Log.e("FAILED", "---$i")
            mPlayer.stopPlay()
        }
        activity?.runOnUiThread {
            loading_progress.visibility = View.GONE
            if (status == HikVideoPlayerCallback.Status.FAILED || status == HikVideoPlayerCallback.Status.EXCEPTION) {
                Log.e("FAILED", "---$i")
                play_t_err.text = MessageFormat.format("播放异常，错误码：{0}", Integer.toHexString(i))
            }
        }
    }
    private val repository:VideoRepository= VideoRepository()
    private lateinit var mPlayer: HikVideoPlayer
    private var isResume = true

    override fun initView() {

        mPlayer = HikVideoPlayerFactory.provideHikVideoPlayer()
        video_back.setOnClickListener { findNavController().popBackStack() }
        tv_play.surfaceTextureListener = object : TextureView.SurfaceTextureListener {
            override fun onSurfaceTextureAvailable(
                surface: SurfaceTexture,
                width: Int,
                height: Int
            ) {
                mPlayer.setSurfaceTexture(surface)
            }

            override fun onSurfaceTextureSizeChanged(
                surface: SurfaceTexture,
                width: Int,
                height: Int
            ) {

            }

            override fun onSurfaceTextureDestroyed(surface: SurfaceTexture): Boolean {
                return false
            }

            override fun onSurfaceTextureUpdated(surface: SurfaceTexture) {

            }
        }
        tv_play.setOnClickListener(View.OnClickListener {
            if (isResume) {
                if (activity?.window?.decorView?.systemUiVisibility === View.INVISIBLE) {
                }
                isResume = false
            } else {
                activity?.window?.decorView?.systemUiVisibility = View.INVISIBLE
                isResume = true


            }
        })

    }

    override fun initData() {
        val bundle=arguments!!
        val cameraIndexCode:String = bundle.getString("cameraIndexCode")!!
        viewModel.getUrl(cameraIndexCode,0).observe(this, Observer {
            startplay(it)
        })
    }

    private fun startplay(url: String) {
        object : Thread() {
            override fun run() {
                if (!mPlayer.startRealPlay(url, this@VideoPlayFragment)) {
                    onPlayerStatus(HikVideoPlayerCallback.Status.FAILED, mPlayer.lastError)
                }

            }
        }.start()
    }


    override fun onPause() {
        object : Thread() {
            override fun run() {
                mPlayer.stopPlay()
            }
        }.start()

        super.onPause()
    }
}
